#include "allocation.h"
#include "base.h"

#include <stdlib.h>

namespace l8
{

#ifdef DEBUG

static void* invalid = static_cast<void*>(NULL);

void* Embedded::operator new(size_t size) {
  UNREACHABLE();
  return invalid;
}


void Embedded::operator delete(void* p) {
  UNREACHABLE();
}


void* AllStatic::operator new(size_t size) {
  UNREACHABLE();
  return invalid;
}


void AllStatic::operator delete(void* p) {
  UNREACHABLE();
}

#endif


int NativeAllocationChecker::allocation_disallowed_ = NativeAllocationChecker::ALLOW;


void* Malloced::New(size_t size)
{
    ASSERT(NativeAllocationChecker::allocation_allowed());
    void * result = malloc(size);
    if (result == NULL)
    {
        ASSERT(!"Malloced operator new");
        FatalProcessOutOfMemory();
    }

    return result;
}


void Malloced::Delete(void* p)
{
    free(p);
}


void Malloced::FatalProcessOutOfMemory()
{
    ASSERT(!"Out of memory");
    abort();
}

} // l8
